package com.arvato.wxwork.controller.admin.ncc;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.wxwork.bean.bean_do.ncc.WxworkGreetDo;
import com.arvato.wxwork.bean.bean_vo.ncc.WxworkGreetVo;
import com.arvato.wxwork.service.ncc.WxworkGreetService;


/**
 * 企业微信KPI管理Controller
 * @author zhan655
 *
 */
@Controller
@RequestMapping("/admin/wxwork/ncc/greet")
public class WxworkNccAdminController {
	
	private Logger logger = LoggerFactory.getLogger(WxworkNccAdminController.class);

	@Autowired
	private WxworkGreetService wxworkGreetService;
	
	/**
	 * 获取问候语列表
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/list")
	public PageData listGreet(HttpServletRequest req, @RequestParam Map<String, Object> param) {
		Query query = new Query(param);
		logger.info("获取kpi列表");
		PageData pageData = wxworkGreetService.listGreetByQuery(query, query.getPageNum(), query.getPageSize());
		return pageData;
	}
	
	/**
	 * 获取一个问候语的详情
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/detail")
	public Result GreetDetail(String id) {
		WxworkGreetVo greetVo = wxworkGreetService.getGreetById(id);
		return Result.success(greetVo);
	}
	
	/**
	 * 创建问候语
	 * @param req
	 * @param greetDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/insert")
	public Result create(HttpServletRequest req, WxworkGreetDo greetDo) {
		logger.info("新增一个问候语");
		// 一个品牌一个活动只能有一个默认的问候语
		boolean flag = wxworkGreetService.check(greetDo.getBrand(), greetDo.getCampaign(), greetDo.getType());
		if(flag) {
			return Result.fail("一个品牌活动只能有一个默认问候语。");
		}
		try {
			greetDo.setCreateTime(new Date());
			int row = wxworkGreetService.insert(greetDo);
			if(row == 1) {
				return  Result.success("创建成功。");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail("创建失败：" + e.getMessage());
		}
		return Result.success("创建成功。");
	}
	
	/**
	 * 更新问候语
	 * @param req
	 * @param GreetDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/update")
	public Result update(HttpServletRequest req, WxworkGreetDo greetDo) {
		logger.info("更新一个问候语");
		try {
			int row = wxworkGreetService.update(greetDo);
			if(row == 1) {
				return  Result.success("更新成功。");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail("更新失败：" + e.getMessage());
		}
		return Result.success("更新成功。");
	}
	
	/**
	 * 删除问候语
	 * @param req
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/delete")
	public Result delete(HttpServletRequest req, String id) {
		logger.info("删除一个问候语项");
		try {
			int row = wxworkGreetService.delete(id);
			if(row == 1) {
				return  Result.success("删除成功。");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail("删除失败：" + e.getMessage());
		}
		return Result.success("删除成功。");
	}
	
	
	/**
	 * 客户信息管理
	 * @param req
	 * @param GreetId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/customerInfo")
	public PageData customerInfo(HttpServletRequest req, @RequestParam Map<String, Object> param) {
		Query query = new Query(param);
		logger.info("获取客户信息管理列表");
		PageData pageData = wxworkGreetService.customerInfo(query, query.getPageNum(), query.getPageSize());
		return pageData;
	}
	
	
}

